<?php
namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
use qiniu\Qn;
use think\captcha\facade\Captcha;
class Login extends Base{
    //生成验证码
    public function verify(){
        $id=mt_rand(100000, 999999);//验证码标识
        $uniqid = uniqid("$id");
        //返回数据 验证码图片路径、验证码标识
        $this->info['data'] = [
            'src' =>captcha_src($uniqid),
            'uniqid' => $uniqid
        ];
        return json($this->info);
    }


    //登录信息校验
    public function login(){
        $pt = input("param.");
        $captcha = new Captcha();
        if($captcha::check($pt['imgCode'])){

            //密码登录
            if($pt['type'] == '1'){
                return $this->seekMember([
                    'email'=>trim($pt['email']),
                    'pass'=>md5('anan'.md5(trim($pt['pass'])))
                ]);
            }

            //邮箱登录
            if($pt['type'] == '2'){
                if($pt['code'] == '999999'){
                    return $this->seekMember([
                        'email'=>trim($pt['email'])
                    ]);
                }else{
                    $this->info['code'] = 201;
                    $this->info['msg'] = '邮箱验证码有误';
                    return json($this->info);
                }
            }
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '图形验证码有误';
            return json($this->info);
        }
    }

    //根据登录信息 查找用户
    public function seekMember($where){
        $admin = Db::table('an_member')->where($where)->find();
        if($admin){
            Db::table('an_member')->where('id', $admin['id'])->data(['login_time'=>time()])->update();
            $this->info['code'] = 200;
            $this->info['msg'] = '登录成功';
            $this->info['data']['user'] = $admin;
            $this->info['data']['token'] = signToken($admin['id']);
            session('memberData', serialize($admin));
            return json($this->info);
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '登录失败,账号或密码有误';
            return json($this->info);
        }
    }

    //发送邮箱验证码
    public function set_eamil_code(){
        $pt = input("param.");
        $captcha = new Captcha();
        if($captcha::check($pt['imgCode'])){

            $res = $this->getResult(1,30);
            if (empty($res)) {
                $this->info['code']=201;
                $this->info['msg']='请求太快，请30秒之后再试';
                return json($this->info);
            }

            send_email($pt['email'],'安之超级系统','您的验证码是999999');
            return json($this->info);  
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '图形验证码有误';
            return json($this->info);
        }
    }

    //注册
    public function reg(){
        $pt = input("param.");
        $captcha = new Captcha();
        if($captcha::check($pt['imgCode'])){
            if($pt['code'] == '999999'){
                $formData = [
                    'email'=>$pt['email'],
                    'money'=>'0.00',
                    'pass'=>md5('anan'.md5(trim($pt['passA']))),
                    'create_time'=>time()
                ];
                $formData['id'] = Db::table('an_member')->insertGetId($formData);
        
                $this->info['data'] = $formData;
                $this->info['msg'] = '新用户';
                $this->info['data']['id'] =  Db::name('an_member')->getLastInsID();
                return json($this->info);
            }else{
                $this->info['code'] = 201;
                $this->info['msg'] = '邮箱验证码有误';
                return json($this->info);
            }
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '图形验证码有误';
            return json($this->info);
        }
    }

    //重置密码
    public function pass(){
        $pt = input("param.");
        $captcha = new Captcha();
        if($captcha::check($pt['imgCode'])){
            if($pt['code'] == '999999'){
                $memberData = Db::table('an_member')->where('email', $pt['email'])->find();
                if($memberData){
                    Db::table('an_member')->where('id',$memberData['id'])->data([
                        'pass'=>md5('anan'.md5(trim($pt['passA'])))
                    ])->update();
                    $this->info['code'] = 200;
                    $this->info['msg'] = '重置成功';
                }else{
                    $this->info['code'] = 201;
                    $this->info['msg'] = '重置失败，账号不存在';
                }
                return json($this->info);
            }else{
                $this->info['code'] = 201;
                $this->info['msg'] = '邮箱验证码有误';
                return json($this->info);
            }
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '图形验证码有误';
            return json($this->info);
        }
    }

    /**
     * 使用jwt生成token字符串
     */
    public function getToken(){
        //从header里获取token
        $token= request()->header('token');

        //检查token
        $result = checkToken($token);

        $this->info['data'] = signToken($result['data']['uid']);
        return json($this->info);
    }

    //上传七牛云文件
    public function upQiniuFile($prefix='family'){
        $qn = new Qn();
        $img = $qn->upFile(request(),$prefix);
        $this->info['code'] = 200;
        $this->info['data'] = $img;
        return json($this->info);
    }
}
